/*
 * $Id: ChangeSpecification.java 23 2002-09-29 13:59:30Z bpruessmann $
 * 
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 * 	  David Corbin  - Boris write the method, I just moved it here
 */
package net.sourceforge.p4eclipse.core.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sourceforge.p4eclipse.core.Policy;
import net.sourceforge.p4eclipse.core.resources.LocalFile;

/**
 * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
 * @author dcorbin
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class ChangeSpecification
{

	public static String createChangeSpecification(String changelist, String description, LocalFile[] files)
	{
		Policy.assertNotNull(changelist);
		Policy.assertNotEmpty(changelist);
		Policy.assertNotNull(description);
		Policy.assertNotNull(files);
				
		List depotFileNames = new ArrayList();
		for (int i = 0, length = files.length; i < length; i++)
			depotFileNames.add(files[i].getSyncInfo().getDepotFileName());

		//description = description.replaceAll("\n", "\n\t"); only on JDK 1.4 available
		StringBuffer descriptionBuffer = new StringBuffer();
		int lf = description.indexOf('\n');
		if (lf != -1)
		{
			int last = 0;
			while (lf != -1)
			{
				descriptionBuffer.append(description.substring(last, lf)).append('\t');
				last = lf + 1;
				lf = description.indexOf('\n', last);
			}
			descriptionBuffer.append(description.substring(last));
			description = descriptionBuffer.toString();
		}

		
		StringBuffer result = new StringBuffer(1024);
		
		// XXX TODO: Make line separator platform independent.
		result.append("Change:").append("\n\t").append(changelist).append("\n\n");
		result.append("Description:").append("\n\t").append(description).append("\n\n");
		result.append("Files:").append("\n");
		
		for (Iterator it = depotFileNames.iterator(); it.hasNext(); )
		{
			String depotFileName = (String)it.next();
			result.append("\t").append(depotFileName).append("\n");
		}
		result.append("\n");
				
		return result.toString();
	}
	


}

